// editaddress.js
var utils = require('../../utils/address.js');
var app = getApp()
var host = app.globalData.host
Page({
  data: {
    id: null,
    comefrom:'',
    name: null,
    mobile: null,
    province: null,
    city: null,
    county: null,
    address: null,
    isDefaultAddress: false,
    thirdpartyId: null,
    pIndex: 0,
    cIndex: 0,
    aIndex: 0,
    pArray: [],
    cArray: [],
    aArray: [],
    pValue: '',
    cValue: '',
    aValue: ''
  },
  refresh(pIndexDefault, cIndexDefault, aIndexDefault) {
    var that = this
    var pArray = utils.getProvince();

    var pIndex = pIndexDefault;
    var pValue = pArray[pIndexDefault]
    if (pIndex == -1) {//如果第一次进入
      for (var x in pArray) {
        console.log('pArray[x] = ' + pArray[x])
        console.log('that.data.province = ' + that.data.province)
        if (pArray[x] == that.data.province) {
          pIndex = x;
          pValue = pArray[pIndex];
          break;
        }
      }
    }



    var cArray = utils.getCity(pArray[pIndex])//得到市级的列表
    var cIndex = cIndexDefault
    var cValue = cArray[cIndex]
    if (cIndex == -1) {
      for (var y in cArray) {
        if (cArray[y] == that.data.city) {
          cIndex = y;
          cValue = cArray[cIndex];
          break;
        }
      }
    }

    var aArray = utils.getArea(pValue, cValue)
    var aIndex = aIndexDefault
    var aValue = aArray[aIndex]
    if (aIndex == -1) {
      for (var z in aArray) {
        if (aArray[z] == that.data.county) {
          aIndex = z;
          aValue = aArray[aIndex];
          break;
        }
      }
    }


    that.setData({
      pArray: pArray,
      pIndex: pIndex,
      pValue: pValue,
      cArray: cArray,
      cIndex: cIndex,
      cValue: cValue,
      aArray: aArray,
      aIndex: aIndex,
      aValue: aValue
    })

  },
  bindPickerChangeOfProvince: function (e) {
    var that = this
    var pIndex = e.detail.value//选择的省份编号
    that.refresh(pIndex, 0, 0)
  },

  bindPickerChangeOfCity: function (e) {
    var that = this
    var cIndex = e.detail.value
    that.refresh(that.data.pIndex, cIndex, 0)
  },
  bindPickerChangeOfArea: function (e) {
    var that = this
    var aIndex = e.detail.value
    that.refresh(that.data.pIndex, that.data.cIndex, aIndex)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var openId = wx.getStorageSync('openId')
    var that = this
    var itemid = options.itemid
    var comefrom = options.comefrom

    wx.removeStorageSync("selectedAddr")
    wx.removeStorageSync("toEditAddr")

    wx.request({
      url: host + "/addressApi/getAddress",
      data: {
        id: itemid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var source = res.data.data
        var x = false;
        if (source.isDefault == 1) x = true;

        that.setData({
          id: source.id,
          name: source.name,
          mobile: source.mobile,
          province: source.province,
          city: source.city,
          county: source.county,
          address: source.address,
          isDefaultAddress: x,
          comefrom: comefrom
        })

        that.refresh(-1,-1,-1)

      }
    })



  },
  // deleteAddress: function (e) {
  //   var that = this
  //   wx.showModal({
  //     title: '提示',
  //     content: '确定要删除？',
  //     success: function (res) {
  //       if (res.confirm) {
  //         var source = e.detail.value
  //         var addrId = source.id
  //         var isDefault = that.data.isDefaultAddress ? "1" : "0"

  //         wx.request({
  //           url: host + "/addressApi/delAddress", //仅为示例，并非真实的接口地址
  //           data: {
  //             thirdpartyId: wx.getStorageSync('openId'),
  //             isDefault: isDefault,
  //             id: source.id
  //           },
  //           header: {
  //             'content-type': 'application/json'
  //           },
  //           success: function (res) {
  //             wx.showToast({
  //               title: '删除成功',
  //               icon: 'success',
  //               duration: 2000
  //             })
  //             wx.navigateBack({
  //               delta: 1
  //             })
  //           }
  //         })
  //       } else if (res.cancel) {
  //         wx.showToast({
  //           title: '用户已取消',
  //           icon: 'success',
  //           duration: 2000
  //         })
  //       }
  //     }
  //   })
  // },
  setAddressStatus: function (e) {
    var that = this
    that.setData({
      isDefaultAddress: e.detail.value
    })
  },
  formSubmit: function (e) {
    var that = this
    var isDefaultAddr = that.data.isDefaultAddress ? "1" : "0"
    var source = e.detail.value
    wx.request({
      url: host + "/addressApi/address", //仅为示例，并非真实的接口地址
      data: {
        id: source.id,
        name: source.name,
        mobile: source.mobile,
        province: that.data.pValue,
        city: that.data.cValue,
        county: that.data.aValue,
        address: source.address,
        isDefault: isDefaultAddr,
        thirdpartyId: wx.getStorageSync('openId')
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
        // wx.navigateBack({
        //   delta: 1
        // })
        if(that.data.comefrom != '')
          wx.redirectTo({
            url: '../personaddress/personaddress?comefrom='+that.data.comefrom
          })
        else 
          wx.navigateBack({
            delta: 1
          })
      }
    })
  }
})